﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yule8xCommon.Applib;
using Yule8xCommon.Db;
using Yule8xCommon.Typelib;
using Yule8xModel;
using Yule8xWechat.Applib;

namespace Yule8xWechat.Agent
{
    public partial class Child2Agent : System.Web.UI.Page
    {
        protected System.Data.DataTable dtList;
        protected WxUserInfoResult wxUser;
        protected int todaycount, totalcount, user_id;
        protected decimal todayamount, totalamount;
        protected void Page_Load(object sender, EventArgs e)
        {
            user_id = RequestUtil.GetInteger("user_id");
            if (RuntimeDataUtil.CheckDebugModel() && user_id > 0)
            {
                wxUser = UserModels.GetWxUserInfo(user_id);
                if (null == wxUser)
                {
                    ResponseUtil.WriteEnd("user_id无效!");
                }
            }
            else
            {
                wxUser = LoginPageBase.GetLoginInfo();
                if (null == wxUser)
                {
                    Response.Redirect(WechatUrls.GetUserInfoUrl("agent"), true);
                }
            }
            /*
            object loginData = Session["login"];
            if (null != loginData)
            {
                wxUser = loginData as WxUserInfoResult;
            }
            if (null == loginData || null == wxUser)
            {
                Response.Redirect(WechatUrls.GetUserInfoUrl("agent"), true);
            }
            */
            string sqlstmt = string.Format("select child2_agent_count_today, child2_agent_count, child2_agent_income_today, child2_agent_income_total from t_user_agent_statistics where user_id={0}", wxUser.user_id);
            System.Data.DataTable dt = DbFactory.Init(AppInfo.conn_yule8x_agent).ExecuteDataTable(sqlstmt);
            todaycount = DataTableUtil.GetInteger(dt, "child2_agent_count_today");
            totalcount = DataTableUtil.GetInteger(dt, "child2_agent_count");
            // 2级代理今日收入统计
            sqlstmt = string.Format("select sum(rebate_amount_today) from t_agent2_statistics where user_id={0}", wxUser.user_id);
            todayamount = decimal.Parse(DbFactory.Instance.GetValue(sqlstmt, "0").ToString());
            //todayamount = DataTableUtil.GetDecimal(dt, "child2_agent_income_today");
            // 2级代理累计收入统计
            sqlstmt = string.Format("select sum(rebate_amount_total) from t_agent2_statistics where user_id={0}", wxUser.user_id);
            totalamount = decimal.Parse(DbFactory.Instance.GetValue(sqlstmt, "0").ToString());

            sqlstmt = string.Format("select u.user_id, u.nickname, s.rebate_amount_today, s.rebate_amount_total from t_agent2_statistics s left join t_user u on s.agent2_id=u.user_id where s.user_id={0}", wxUser.user_id);
            dtList = DbFactory.Instance.ExecuteDataTable(sqlstmt);
        }

        protected string ShortText(object value, int length, string defv = "-")
        {
            return StringUtil.Substring(value, length, defv);
        }
    }
}